/*
Released under this MIT License:
		
Copyright (c) 2010 Bart van der Schoor
		
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
		
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
		
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
	
 */
package bruce
{

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.media.Sound;
	import flash.system.ApplicationDomain;
	import flash.utils.getDefinitionByName;

	/**
	 *
	 *	Library
	 *	
	 *	 grab stuff from the library Library.newMovieClip() (specifiy a Class there first oc)
	 *	
	 *	part of the Bruce Simple AS3 Banner Toolkit
	 *	
	 *	status: looks good, needs testing, polish (target/addChild)
	 *	 
	 * @author Bart van der Schoor @ GML
	 * @version 0.6
	 */
	public class Library
	{
		// TODO needs cross-swf/domain checking
		private static const eUnknownLink:String = ' unknown';
		// expose internal to LoadLibrary
		internal static var _domains:Array = new Array();
		internal static var _cache:Object = new Object();
		private static const TYPE:String = 'Library..';

		public static function newMovieClip(linkage:String):MovieClip
		{
			return MovieClip(Library.getObject(linkage));
		}

		public static function getSound(linkage:String):Sound
		{
			return Sound(Library.getObject(linkage));
		}

		public static function getObject(linkage:String):Object
		{
			var def:Class = Library.getClass(linkage);
			if (def)
			{
				return Object(new def());
			}
			Bruce.arg(TYPE + 'getObject(' + linkage + ')' + Library.eUnknownLink);
			return null;
		}

		public static function getClass(linkage:String):Class
		{
			if (!Library.hasItem(linkage))
			{
				Bruce.arg(TYPE + 'getClass(' + linkage + ')' + Library.eUnknownLink);
				return null;
			}
			return _cache[linkage];
		}

		public static function hasItem(linkage:String):Boolean
		{
			if (!linkage)
			{
				Bruce.arg(TYPE + 'hasItem() linkage' + Bruce.eEmpty);
				return false;
			}

			var def:Class = getDefinitionByName(linkage) as Class;
			var obj:Object;
			if (def == null)
			{
				for each (var domain:ApplicationDomain in _domains)
				{
					obj = domain.getDefinition(linkage);
					if (obj)
					{
						_cache[linkage] = obj;
						return true;
					}
				}
			}
			return false;
		}

		public static function addLibrary(clip:DisplayObject):void
		{
			if (!clip)
			{
				Bruce.arg(TYPE + 'grab()' + Bruce.eclNull);
				return;
			}
			if (!clip.root)
			{
				Bruce.arg(TYPE + 'grab(' + clip + ') clip is not on a root');
				return;
			}

			try
			{
				if (clip.root.loaderInfo.applicationDomain)
				{
					_domains.push(clip.root.loaderInfo.applicationDomain);
					// leaving!
					return;
				}
			}
			catch(error:Error)
			{
				// SecurityError problabably
				Bruce.log(error);
			}
		}

		public static function getBitmap(linkage:String, smoothing:Boolean = true, snapping:String = 'auto'):Bitmap
		{
			snapping = snapping ? snapping : 'auto';

			var def:Class = Library.getClass(linkage);
			if (def)
			{
				// custom params..
				return new Bitmap(BitmapData(new def(null, null)), snapping, smoothing);
			}
			Bruce.arg(TYPE + 'getBitmap(' + linkage + ')' + Library.eUnknownLink);
			return null;
		}
	}
}
